package com.captjack.shiro.configuration;

import com.captjack.shiro.component.CustomizeSessionManager;
import com.captjack.shiro.component.CustomizeShiroRealm;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Description description.....
 *
 * @author Jack Sparrow
 * @version 1.0.0
 * @date 2018/6/7 22:39
 * package com.captjack.shiro.configuration
 */
@Configuration
@Order(value = 1)
public class CustomizeShiroConfiguration {

    /**
     * 权限管理，配置主要是Realm的管理认证
     */
    @Bean(name = "shiroSecurityManager")
    SecurityManager securityManager(CustomizeShiroRealm customizeShiroRealm, @Qualifier(value = "redisSessionManager") CustomizeSessionManager customizeSessionManager) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //
        securityManager.setRealm(customizeShiroRealm);
        //
        securityManager.setSessionManager(customizeSessionManager);
        return securityManager;
    }

    /**
     * Filter工厂，设置对应的过滤条件和跳转条件
     *
     * @param securityManager 安全管理
     * @return 过滤工厂
     */
    @Bean
    ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier(value = "shiroSecurityManager") SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        // 工厂
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        // 登录
        shiroFilterFactoryBean.setLoginUrl("/login.html");
        // 首页
        shiroFilterFactoryBean.setSuccessUrl("/index.html");
        // 错误页面，认证不通过跳转
        shiroFilterFactoryBean.setUnauthorizedUrl("/error.html");
        // 设置过滤链
        shiroFilterFactoryBean.setFilterChainDefinitionMap(getFilterConfiguration());
        return shiroFilterFactoryBean;
    }

    /**
     * 获取配置的过滤链
     *
     * @return 过滤链
     */
    private Map<String, String> getFilterConfiguration() {
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        // 登录请求
        filterChainDefinitionMap.put("/user/login", "anon");
        // 匿名处理
        filterChainDefinitionMap.put("/*.*", "anon");
        // 登出
        filterChainDefinitionMap.put("/user/logout", "logout");
        // 对所有用户认证
        filterChainDefinitionMap.put("/**", "authc");
        return filterChainDefinitionMap;
    }

    /**
     * 加入注解的使用，不加入这个注解不生效
     *
     * @param securityManager 安全管理
     * @return
     */
    @Bean
    AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier(value = "shiroSecurityManager") SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

}
